import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    kw:["柯南2","高考加油1","北京疫情1","明日方舟3","创造营2020","上海堡垒4","4公主连结","3三时三餐","纸人2"],
    mind:[],
    history:[]
  },
  mutations: {
    changeHistory(state,item) {
      state.history.push(item);

      //去重
      var index = state.history.findIndex((ele) => {
        console.log(ele)
        return ele === item;
      });

      //删除
      if (index !== -1) {
        state.history.splice(index,1);
      }

      //将新添加的数据提交到第一个位置
      state.history.unshift(item);

      //搜索记录最多保存三条数据
      if (state.history.length > 3) {
        state.history.pop();
      }
    },

    //联想搜索  关键词
    Kw(state,item) {
      state.mind = [];
      state.kw.forEach(ele => {
        if (ele.includes(item)) {
          state.mind.push(ele);
          console.log(state.mind);
        }
      })
    }
  },
  actions: {
    changeHistory(context,item) {
      context.commit('changeHistory',item)
    },
    Kw(context,item) {
      context.commit('Kw',item);
    }

  },
  modules: {
  }
})
